﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConnectSQLServer.Common;
using Microsoft.SqlServer.Management.Smo;

namespace ConnectSQLServer.Controls
{
    public partial class MyTable : UserControl
    {
        public DataGridView dgv;
        public InfoDatabase info;
        public int typeTable;           //1 - design 2 - data
        public ComboBox cbb = new ComboBox();
        public int rowSelected;
        public int colSelected;
        public string cellValue;
        private bool flagSelected;

        public MyTable(int type, InfoDatabase info)
        {
            InitializeComponent();
            dgv = this.dgvTable;
            this.info = info;
            typeTable = type;
            if (this.typeTable == 1)
            {
                this.dgvTable.Columns.Add("ColumnName", "Column Name");
                this.dgvTable.Columns.Add("DataType", "Data Type");
                DataGridViewCheckBoxColumn cbdvg = new DataGridViewCheckBoxColumn();
                cbdvg.HeaderText = "Allow Nulls";
                this.dgvTable.Columns.Add(cbdvg);
                this.dgvTable.Columns[0].Width = 150;
                this.dgvTable.Columns[1].Width = 100;
                this.dgvTable.Columns[2].Width = 90;
            }
            this.colSelected = -1;
            this.rowSelected = -1;
            this.cellValue = "string.Empty";
            flagSelected = false;

        }

        private void MyTable_Load(object sender, EventArgs e)
        {
            CreateEditControl();
        }

        private void CreateEditControl()
        {
            cbb.DataSource = ConnectSQLServer.Common.DataTypeSQL.DataTypeSources();
            cbb.AutoCompleteCustomSource.AddRange(ConnectSQLServer.Common.DataTypeSQL.DataTypeSources().ToArray());
            cbb.AutoCompleteMode = AutoCompleteMode.Suggest;
            cbb.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbb.Hide();
            this.dgvTable.Controls.Add(cbb);
        }

        private void dgvTable_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cbb.Text = string.Empty;
            //set current value from current cell
            if (this.dgvTable.CurrentCell.Value != null)
                this.cellValue = this.dgvTable.CurrentCell.Value.ToString();
            else
                this.cellValue = string.Empty;
            if (this.typeTable == 1)
            {
                //Save row edit
                if (this.rowSelected != -1 && this.colSelected != -1)
                {
                    if (this.rowSelected != e.RowIndex && GlobalVariable.TCManager.GetTabPageSelected().Tag == "Unsave")
                    {
                        if (this.dgvTable[0, rowSelected].Value != null && this.dgvTable[1, rowSelected].Value != null)
                        {
                            string message = SqlAccessHelper.AlterTable(this.dgvTable.Rows[rowSelected], rowSelected, this.info);
                            if (!string.IsNullOrEmpty(message))
                            {
                                MessageBox.Show("Error: " + message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                this.dgvTable.CurrentCell = this.dgvTable[colSelected, rowSelected];
                                return;
                            }
                        }
                    }
                }

                if (e.ColumnIndex == 1)
                {
                    if(this.dgvTable.CurrentCell.Value == null)
                        this.dgvTable[e.ColumnIndex + 1, e.RowIndex].Value = true;
                    this.cbb.Location = this.dgvTable.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
                    this.cbb.Width = this.dgvTable.Columns[e.ColumnIndex].Width;
                    this.cbb.Show();
                    this.cbb.Focus();
                    if (this.dgvTable.CurrentCell.Value != null)
                        this.cbb.Text = this.dgvTable.CurrentCell.Value.ToString();
                    this.rowSelected = e.RowIndex;
                    this.colSelected = e.ColumnIndex;
                    flagSelected = true;
                }
                if (e.ColumnIndex == 0 || e.ColumnIndex == 2)
                {
                    flagSelected = true;
                    this.rowSelected = e.RowIndex;
                    this.colSelected = e.ColumnIndex;
                }

            }
            if (this.typeTable == 2)
            {
                // to do
            }
        }

        private void dgvTable_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (flagSelected == true)
            {

                if (!string.IsNullOrEmpty(cbb.Text))
                    dgvTable.CurrentCell.Value = cbb.Text;
                cbb.Hide();
                flagSelected = false;
            }
            if (this.dgvTable.CurrentCell.Value != null && this.cellValue != "string.Empty")
            {
                if (this.cellValue != this.dgvTable.CurrentCell.Value.ToString())
                    GlobalVariable.TManager.RowChanged();
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            int r = this.dgvTable.CurrentCell.RowIndex;
            int c = this.dgvTable.CurrentCell.ColumnIndex;
            DataGridViewCell dgvCurrent = this.dgvTable.CurrentCell;

            if (msg.WParam.ToInt32() == (int)Keys.Enter)
            {
                if (r == this.dgvTable.RowCount - 1)
                {
                    return base.ProcessCmdKey(ref msg, keyData);
                }
                else
                {
                    dgvCurrent = this.dgvTable[c, r + 1];
                }
                this.dgvTable.CurrentCell = dgvCurrent;
                this.rowSelected = r;
                this.colSelected = c;
                object sender = new object();
                DataGridViewCellEventArgs ec = new DataGridViewCellEventArgs(this.dgvTable.CurrentCell.ColumnIndex, this.dgvTable.CurrentCell.RowIndex);
                this.dgvTable_CellClick(sender, ec);
                return true;
            }
            if (msg.WParam.ToInt32() == (int)Keys.Tab)
            {
                if (c == this.dgvTable.ColumnCount - 1)
                {
                    if (r == this.dgvTable.RowCount - 1)
                    {
                        return base.ProcessCmdKey(ref msg, keyData);
                    }
                    else
                    {
                        dgvCurrent = this.dgvTable[0, r + 1];
                    }
                }
                else
                {
                    dgvCurrent = this.dgvTable[c + 1, r];
                }
                this.dgvTable.CurrentCell = dgvCurrent;
                object sender = new object();
                DataGridViewCellEventArgs ec = new DataGridViewCellEventArgs(this.dgvTable.CurrentCell.ColumnIndex, this.dgvTable.CurrentCell.RowIndex);
                this.dgvTable_CellClick(sender, ec);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void dgvTable_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            SqlAccessHelper.DeleteColumn(e.Row, e.Row.Index, this.info);
        }
    }
}

